﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="QuadTree.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Collision.
//
//   starLiGHT.Collision is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Collision is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Collision. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 3216                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-01-11 11:05:37 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Collision.BroadPhase
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using starLiGHT.Collections;
    #endregion

    public class QuadTree : AbstractBroadPhase, IBroadPhase
    {
        private QuadTree<QuadTreeCollidableContainer> quadTree;

        public QuadTree()
            : base()
        {
            this.quadTree = new QuadTree<QuadTreeCollidableContainer>(new Vector2(1.0f, 1.0f), 8, false);
        }

        public override void RegisterObject(ICollidable2D c)
        {
            this.quadTree.Insert(new QuadTreeCollidableContainer(c));

            base.RegisterObject(c);
        }

        public override void BuildCollisionPairs(CollisionManager manager, ref ICollidable[] collidables)
        {
            if (collidables == null)
            {
                return;
            }

            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            ICollidable2D a;
            ICollidable2D b;

            // *****************
            // * add new pairs *
            // *****************
            this.TestCount = 0;
            int collidableCount = collidables.Length;
            for (ushort i = 0; i < collidableCount; i++)
            {
                a = collidables[i] as ICollidable2D;
                if (a != null && !a.Static)
                {
                    foreach (QuadTreeCollidableContainer qtcc in this.quadTree.Query(a.WorldBoundingRectangle))
                    {
                        b = qtcc.Collidable as ICollidable2D;
                        if (b != null)
                        {
                            if (!a.Equals(b))
                            {
                                manager.PairManager.AddPair(i, IndexOfCollidable(ref collidables, b), a, b);
                                this.TestCount++;
                            }
                        }
                    }
                }
            }
        }

        public override bool Test(ICollidable2D a, ICollidable2D b)
        {
            if (a == null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == null)
            {
                throw new ArgumentNullException("b");
            }

            return a.WorldBoundingRectangle.Intersects(b.WorldBoundingRectangle);
        }

        public override bool Test(ICollidable3D a, ICollidable3D b)
        {
            throw new NotSupportedException("3D collisions are not supported by the QuadTree broad phase alogrithm.");
        }

        public override bool CheckCompatibility(ICollidable c)
        {
            try
            {
                if (c is ICollidable2D)
                {
                    return ((ICollidable2D)c).LocalBoundingRectangle != null;
                }
            }
            catch
            {
            }

            return false;
        }
    }
}
